package com.pishou.code.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pishou.Data;
import com.pishou.ParamConfig;
import com.pishou.URLConfig;
import com.pishou.code.bean.Comment;
import com.pishou.code.bean.Product;
import com.pishou.code.bean.Video;
import com.pishou.code.csv.CategoryCSV;
import com.pishou.code.csv.ProductCSV;
import com.pishou.code.util.HttpPostClient;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DataDownload extends Thread {

    @Override
    public void run() {
        try{
            String comment_str = HttpPostClient.sendPost(URLConfig.url_comments,"id=" + ParamConfig.getConfig_json().getString("machineId"));
            JSONObject comment_json = JSONObject.parseObject(comment_str);
            if (comment_json.getInteger("success").equals(1)){
                JSONArray array = comment_json.getJSONArray("entity");
                Iterator it = array.iterator();
                Data.comments.clear();
                while (it.hasNext()){
                    JSONObject json = (JSONObject) it.next();
                    Comment comment =  new Comment(json);
                    Data.comments.add(comment);
                    if (new Integer(1).equals(comment.getVideo())){
                        Data.comments_video.add(comment);
                        File file = new File(comment.getCommentVideoPath());
                        if (!file.exists()){
                            Video v = new Video();
                            v.setOss(URLConfig.oss_host + "Buying_show/" + comment.getId() + ".mp4");
                            v.setPath(comment.getCommentVideoPath());
                            v.setName("Comment:" + comment.getId() + ".mp4");
                            new VideoDownload(v).start();
                        }
                    }
                }
            }
            String products_str = HttpPostClient.sendPost(URLConfig.url_products,"id=" + ParamConfig.getConfig_json().getString("machineId")+"&type=count");
            JSONObject products_json = JSONObject.parseObject(products_str);
            if (products_json.getInteger("success").equals(1)){
                JSONArray array = products_json.getJSONArray("entity");
                Iterator it = array.iterator();
                Data.products.clear();
                while (it.hasNext()){
                    JSONObject json = (JSONObject) it.next();
                    Product product = new Product(json);
                    String details_str = HttpPostClient.sendPost(URLConfig.url_details,"sku="+product.getSku());
                    JSONObject details_json = JSONObject.parseObject(details_str);
                    if (details_json.getInteger("success").equals(1)){
                        JSONObject entity = details_json.getJSONObject("entity");
                        product.setDetails(entity);
                    }
                    List<Comment> product_comments = new ArrayList<>();
                    for(Comment comment : Data.comments){
                        if (comment.getSku().equals(product.getSku())){
                            product_comments.add(comment);
                        }
                    }
                    product.setComments(product_comments);
                    Data.products.add(product);
                }
            }
            StringBuffer sb = new StringBuffer(";全部;");
            for (Product product: Data.products){
                if (sb.indexOf(";"+product.getCategory()+";")<0){
                    sb.append(product.getCategory()+";");
                }
            }
            String result_category = sb.substring(1,sb.length()-1);
            Data.categorys = result_category.split(";");

            ProductCSV.setProduct(Data.products);
            CategoryCSV.setCategory(Data.categorys);
        }catch (Exception e){
            e.printStackTrace();
        }
        super.run();
    }
}
